<?php
/*
 * @Create By: Vscode
 * @Author: falcon
 * @Date: 2020-01-01 16:47:12
 * kafka 生产者 和 消费者
 */
namespace application\controller;

use application\depend\Applog;
use application\model\PdoModel;
class delrepeat{


    public function run(){
        $sql = "select distinct wh_code from shelf_manage";
        $result = PdoModel::selectData($sql,[],"");

        foreach($result as $v){
            $delRes = $this->delrepeat($v['wh_code']);
            if(false === $delRes) return "删除重复货架失败了！";
        }

        return "删除重复货架成功！";
    }


    public function delrepeat($whcode){
        $sql = "select shelf,total,wh_code from (select shelf,wh_code,count(*) as total from shelf_manage where wh_code=$whcode group by shelf ) t where shelf in (select shelf from shelf_manage where wh_code=$whcode group by shelf) and total>1";
        $res = PdoModel::selectData($sql,[],"");

        if(!empty($res) && is_array($res)){
            foreach($res as $v){
                $shelf = $v['shelf'];
                $code = $v['wh_code'];
                $count = $v['total'] - 1; //只保留一个
                $sql = "delete from shelf_manage where shelf = '$shelf' and wh_code=$code limit $count";
                $res = PdoModel::selectData($sql,[],""); //执行删除即可
                if(false === $res) {
                    Applog::errorLog("删除失败:".$sql);
                    return false;
                }
            }
        }
        return true;
    }
}